package jk.fox.spring.processor;

import jk.fox.spring.bootstrap.SpringRpcContext;
import jk.fox.spring.scanner.RpcClassPathBeanDefinitionScanner;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.BeanFactoryAware;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.beans.factory.support.BeanDefinitionRegistryPostProcessor;
import org.springframework.util.StringUtils;

import java.util.Set;

/**
 * Rpc服务提供者处理器
 * @see jk.fox.annotaion.RpcService
 * @author cuichao
 * @Description: Pr
 * @Date: create in 2020/5/25 23:42
 */
public class RpcServiceBeanPostProcessor implements BeanDefinitionRegistryPostProcessor,BeanFactoryAware {

    private Set<String> packagesToScan;
    private ConfigurableListableBeanFactory beanFactory;

    public RpcServiceBeanPostProcessor(Set<String> packagesToScan) {
        this.packagesToScan = packagesToScan;
    }

    @Override
    public void postProcessBeanDefinitionRegistry(BeanDefinitionRegistry registry) throws BeansException {
        SpringRpcContext context = beanFactory.getBean(SpringRpcContext.class);
        //构建Rpc扫描器
        RpcClassPathBeanDefinitionScanner scanner = new RpcClassPathBeanDefinitionScanner(registry,context);
        scanner.setBeanFactory(this.beanFactory);
        scanner.scan(StringUtils.toStringArray(packagesToScan));
    }


    @Override
    public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
    }

    @Override
    public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
        this.beanFactory = (ConfigurableListableBeanFactory)beanFactory;
    }
}
